﻿$(document).ready(function () {
    function bindUsers(page) {
        $("#tblUsers tr:not(:first)").remove();
        $("#tblUsers").hide();

        $("#tblUsers").after("<div class='loadingImage'></div>");

        var filterData = {
            "UserPermissionId": $("#FilterUserPermissionId").val()
        };

        //Invoke AJAX method to get the users list
        $.post(pathPrefix + "Users/UsersList", filterData, function (result) {
            if (result.length > 0) {
                $("#tblUsers").show();
            }

            $("#userTemplate").tmpl(result).appendTo("#tblUsers");

            $("#tblUsers").siblings(".loadingImage").remove();

            //loop through the "UserPermissions" cells
            $("#tblUsers td[data-user-permission]").each(function () {
                //preset the permission as the value of the DDL
                var userPermissionsId = $(this).data("userPermission");
                $(this).find("select").val(userPermissionsId);
            });
        }, "json");
    }

    $("#FilterUserPermissionId").change(function () {
        bindUsers(1);
    });

    bindUsers(1);

    //when the user changes one of the input elements in a row
    $("tr :input").live("change", function () {
        var userRow = $(this).parents("tr");

        //get all the user data
        var data = userRow.find(":input").serialize();

        //Invoke AJAX method to update this user (insert or edit)
        $.post(pathPrefix + "Users/UpdateUser", data, function (result) {
            //update the userId value (if inserted row)
            userRow.find("#UserId").val(result);
            userRow.find(".lnkDeleteUser").show();

            userRow.find(".successCell img").fadeIn(1000).delay(2000).fadeOut();
        }, "json");
    });

    //when the user clicks "Delete User"
    $(".lnkDeleteUser").live("click", function () {
        var userRow = $(this).parents("tr");
        var userId = userRow.find("#UserId").val();

        openConfirmDialog("האם אתה בטוח שברצונך למחוק?", function () {
            //Invoke AJAX method to delete this user
            $.post(pathPrefix + "Users/DeleteUser", { "id": userId }, function () {
                userRow.remove();
            }, "json");
        });

        return false;
    });

    $("#btnAddUser").click(function () {
        var mainDialog = updateDialog("משתמש חדש", "350", "500");

        //Invoke AJAX method to get the AddUser dialog
        $.get(pathPrefix + "Users/Add", null, function (result) {
            mainDialog.html(result);
        }, "html");
    });

    $("#frmAddUser").live("submit", function (e) {
        var data = $(this).serialize();

        $.validator.unobtrusive.parse($(this));

        if ($(this).valid()) {
            //Invoke AJAX method to AddUser
            $.post(pathPrefix + "Users/Add", data, function (result) {
                $("#divMainDialog").html(result.Value);

                bindUsers(1);

                //close the dialog
                window.setTimeout(function () {
                    closeDialog();
                }, 3000);
            }, "json");
        }

        e.preventDefault();
    });
});